package com.powernode.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName SysUserController
 * @Description 系统管理员Controller
 * @Date 2024/07/23 22:07
 */
@Api("系统管理员接口管理")
@RequestMapping("/sys/user")
@RestController
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询登录的用户信息
     * @return 信息
     */
    @ApiOperation("查询登录的用户信息")
    @GetMapping("/info")
    public Result<SysUser> loadSysUser() {
        return Result.success(sysUserService.getById(AuthUtils.getLoginUserId()));
    }

    /**
     * 多条件分页查询系统管理员信息
     * @param current  当前页
     * @param size 页面大小
     * @param username 用户名
     * @return 分页结果
     */
    @ApiOperation("多条件分页查询系统管理员信息")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<SysUser>> loadSysUserPage(@RequestParam Long current,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) String username) {
        // 多条件分页查询系统管理员信息
        Page<SysUser> page = sysUserService.page(new Page<>(current, size),
                new LambdaQueryWrapper<SysUser>()
                        .like(!StrUtil.isBlankIfStr(username), SysUser::getUsername, username)
                        .orderByDesc(SysUser::getCreateTime));
        return Result.success(page);
    }

    /**
     * 新增系统管理员信息
     * @param sysUser 系统管理员信息
     * @return 新增结果
     */
    @ApiOperation("新增系统管理员信息")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.saveSysUser(sysUser);
        return Result.handle(count > 0);
    }

    /**
     * 根据标识查询系统管理员信息
     * @param id 标识
     * @return 信息
     */
    @ApiOperation("根据标识查询系统管理员信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable Long id) {
        return Result.success(sysUserService.querySysUserInfoById(id));
    }

    /**
     * 修改管理员信息
     * @param sysUser 管理员信息
     * @return 修改结果
     */
    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.modifySysUser(sysUser);
        return Result.handle(count > 0);
    }

    /**
     * 批量删除系统管理员信息
     * @param userIds 标识集合
     * @return 删除结果
     */
    @ApiOperation("批量删除系统管理员信息")
    @DeleteMapping("/{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUsers(@PathVariable List<Long> userIds) {
        Integer count = sysUserService.removeSysUsersByUserIdList(userIds);
        return Result.handle(count == userIds.size());
    }
}
